import { Component, OnInit } from '@angular/core';
import { StorageService } from 'src/app/sevices/storage.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.scss']
})
export class CartComponent implements OnInit {
  public register = false; // 用户是否登录
  public user = this.storage.getItem('user'); // 获取用户信息
  public cartList = []; // 购物车商品列表
  public ifAll = false; // 是否全选商品
  public money = 0; // 选中商品的总金额
  public num = 0; // 选中商品的件数
  public chooseNum = 0; // 点亮按钮个数
  public ask = 0; // 是否显示询问框
  public deleteId = 0; // 要删除商品的id

  constructor(
    private storage: StorageService,
    public http: HttpClient,
    public router: Router
  ) { }

  ngOnInit() {
    if (this.storage.getItem('user')) {
      this.register = true;
      // 表示用户已经登录，获取服务器中用户的购物车商品
      const url = 'http://192.168.2.228:8080/shoppingcart/personal?user_id=' + this.user.u_id;
      this.http.get(url).subscribe((Response: any) => {
        this.cartList = Response.data;
        // tslint:disable-next-line: prefer-for-of
        for (let i = 0; i < this.cartList.length; i ++) {
          // tslint:disable-next-line: no-string-literal
          this.cartList[i]['static'] = 0; // 0表示未选中1为选中
          // tslint:disable-next-line: no-string-literal
          this.cartList[i]['delete'] = 0; // 0代表未删除1代表删除
        }
      });
    }
  }
  //  改变商品的按钮以及将商品加入结算清单
  // 全选按钮
  All() {
    if (this.ifAll === false) { // 如果商品没有被全选
      this.ifAll = true;  // 改变全选按钮
      // tslint:disable-next-line: prefer-for-of
      for (let i = 0; i < this.cartList.length; i++) { // 全选商品
        // tslint:disable-next-line: no-string-literal
        if (this.cartList[i]['static'] === 0) { // 选中该商品
          // tslint:disable-next-line: no-string-literal
          this.money += (this.cartList[i]['g_price'] * this.cartList[i]['goods_count']); // 将选中商品的金额加入结算中
          // tslint:disable-next-line: no-string-literal
          this.num += this.cartList[i]['goods_count']; // 加上选中件数
          // tslint:disable-next-line: no-string-literal
          this.cartList[i]['static'] = 1;  // 按钮变绿
          this.chooseNum += 1; // 选中加一
        }
      }
    } else { // 取消全选
      this.ifAll = false;  // 改变全选按钮
      // tslint:disable-next-line: prefer-for-of
      for (let i = 0; i < this.cartList.length; i++) {
        // tslint:disable-next-line: no-string-literal
        if (this.cartList[i]['static'] === 1) { // 取消选中该商品
          // tslint:disable-next-line: no-string-literal
          this.money -= (this.cartList[i]['g_price'] * this.cartList[i]['goods_count']); // 从结算中将选中商品的金额减去
          // tslint:disable-next-line: no-string-literal
          this.num -= this.cartList[i]['goods_count']; // 减去选中件数
          // tslint:disable-next-line: no-string-literal
          this.cartList[i]['static'] = 0; // 按钮变白
          this.chooseNum -= 1; // 取消选中减一
        }
      }
    }
  }
    // 单选
  sum(i) {
    // tslint:disable-next-line: no-string-literal
    if (this.cartList[i]['static'] === 0) { // 选中该商品
      // tslint:disable-next-line: no-string-literal
      this.money += (this.cartList[i]['g_price'] * this.cartList[i]['goods_count']); // 将选中商品的金额加入结算中
      // tslint:disable-next-line: no-string-literal
      this.num += this.cartList[i]['goods_count']; // 加上选中件数
      // tslint:disable-next-line: no-string-literal
      this.cartList[i]['static'] = 1;  // 按钮变绿
      this.chooseNum += 1; // 选中加一
    } else { // 取消选中该商品
      // tslint:disable-next-line: no-string-literal
      this.money -= (this.cartList[i]['g_price'] * this.cartList[i]['goods_count']); // 从结算中将选中商品的金额减去
      // tslint:disable-next-line: no-string-literal
      this.num -= this.cartList[i]['goods_count']; // 减去选中件数
      // tslint:disable-next-line: no-string-literal
      this.cartList[i]['static'] = 0; // 按钮变白
      this.chooseNum -= 1; // 取消选中减一
    }
  }
  // 商品被选
  choose(id) { // 参数为选择商品的static,用于控制绿点
    this.which(id);
  }
  // 判断哪个商品被选，并改变按钮状态
  which(id) {
    // 寻找对应的商品
    // tslint:disable-next-line: prefer-for-of
    for (let i = 0; i < this.cartList.length; i ++) {
      // tslint:disable-next-line: no-string-literal
      if (this.cartList[i]['g_id'] === id) { // id对应找到相应的商品
        this.sum(i);
      }
    }
    if (this.chooseNum === this.cartList.length) {
      this.ifAll = true;

    } else {
      this.ifAll = false;
    }
  }
  // 删除商品
  delete(id) {
    this.ask = 1;
    this.deleteId = id;
  }
  // 询问框弹出后点击确认
  deleteYes(id) {
    id = this.deleteId;
    const url = 'http://192.168.2.228:8080/shoppingcart/delete';
    const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
    this.http.post(url, { user_id: this.user.u_id, goods_ids: [id] }, httpOptions).subscribe((response: any) => {
      if (response.message === '成功') {
        // tslint:disable-next-line: prefer-for-of
        for (let i = 0; i < this.cartList.length; i ++) {
          // tslint:disable-next-line: no-string-literal
          if ((this.cartList[i]['g_id']) === id) { // id对应找到相应的商品
            // tslint:disable-next-line: no-string-literal
            this.cartList[i]['delete'] = 1; // 商品从购物车里消失
          }
        }
      }
    });
    this.ask = 0;
  }
  // 取消删除
  deleteNo() {
    this.ask = 0;
  }
}
